#!/usr/bin/env python3

# Copyright (c) 2020-2021 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


from ..._util import Util, WinRegistry
from ..._prototype import CustomAction


class ShowHideDesktopIcons(CustomAction):

    _iconToGuid = {
        "my-computer": "{20D04FE0-3AEA-1069-A2D8-08002B30309D}",
        "my-document": "{2CC5CA98-6485-489A-920E-B3E88A6CCCE3}",
        "recycle-bin": "{645FF040-5081-101B-9F08-00AA002F954E}",
        "control-panel": "{5399E694-6CE5-4D6C-8FCE-1D8870FDCBA0}",
        "network": "{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}",
    }

    def __init__(self, data, for_user=None):
        for k in data.keys():
            assert k in self._iconToGuid.keys()
        self._data = data

        self._user = for_user

    def run(self, action_name, s, ts, work_dir):
        with work_dir.mount_image_file() as mp:
            reg = WinRegistry(mp.drive_c_dir)
            for u in Util.getListByItem(reg.get_users(), self._user):
                for icon, bShowHide in self._data.items():
                    reg.add_or_modify("HKU\\%s\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\HideDesktopIcons\\NewStartPanel" % (u),
                                      self._iconToGuid[icon],
                                      "REG_DWORD",
                                      Util.boolToInt01(not bShowHide))

    def get_after(self):
        return []

    def get_before(self):
        return []


class ChangeExplorerSettings(CustomAction):

    def __init__(self, data, for_user=None):
        theMap = [
            "show-hidden",
            "hide-extensions-for-known-file-types",
        ]
        for k in data.keys():
            assert k in theMap.keys()
        self._data = data

        self._user = for_user

    def run(self, action_name, s, ts, work_dir):
        with work_dir.mount_image_file() as mp:
            reg = WinRegistry(mp.drive_c_dir)
            for u in Util.getListByItem(reg.get_users(), self._user):
                for k, v in self._data.items():
                    if k == "show-hidden":
                        reg.add_or_modify("HKU\\%s\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced" % (u),
                                          "Hidden",
                                          "REG_DWORD",
                                          Util.boolToInt01(v))
                    elif k == "hide-extensions-for-known-file-types":
                        reg.add_or_modify("HKU\\%s\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced" % (u),
                                          "HideFileExt",
                                          "REG_DWORD",
                                          Util.boolToInt01(v))
                    else:
                        assert False

    def get_after(self):
        return []

    def get_before(self):
        return []
